import wepy from 'wepy'
export default class extends wepy.mixin {
  onLoad(option) {
    console.log(option)
    this.goods_id = option.goods_id
    this.getGoodsDetailList()
  }
  data = {
    goods_id: '',
    goodsInfo: {},
    address: null
  }
  methods = {
    preview(index) {
      console.log(index)

      wepy.previewImage({
        urls: this.goodsInfo.pics.map((x) => x.pics_big),
        current: index
      })
    },
    async city() {
      const res = await wepy.chooseAddress().catch((err) => err)
      console.log(res)
      if (res.errMsg === 'chooseAddress:fail cancel') {
        return
      }
      this.address = res
    },
    addToCart() {
      console.log(this.goodsInfo)
      this.$parent.addCart(this.goodsInfo)
      wepy.showToast({
        title: '已加入购物车',
        icon: 'success'
      })
    }
  }
  computed = {
    addressCity() {
      if (this.address === null) {
        return '请选择收货地址'
      }
      const adds = this.address
      const str = adds.provinceName + adds.cityName + adds.countyName + adds.detailInfo
      return str
    },
    cartNum() {
      let c = 0
      this.$parent.globalData.cart.forEach((element) => {
        if (element.isCheck) {
          c += element.count
        }
      })
      return c
    }
  }
  async getGoodsDetailList() {
    const { data: res } = await wepy.getData('/goods/detail', {
      goods_id: this.goods_id
    })
    console.log(res)
    this.goodsInfo = res.message
    this.$apply()
  }
}
